﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Workflow.ComponentModel;
using Microsoft.SharePoint.Workflow;
using Microsoft.Office.Word.Server.Service;
using Microsoft.SharePoint.Administration;

namespace SP2010WordAutomation.Workflow
{
    internal class Common
    {
        /// <summary>
        /// logs exceptions to sharepoint's workflow history log
        /// </summary>
        /// <param name="e">Exception to log</param>
        /// <param name="context">Current ActivityExecutionContext</param>
        /// <param name="workflowInstanceID">Current WorkflowInstanceId</param>
        internal static void LogExceptionToWorkflowHistory(Exception e, ActivityExecutionContext context,
                                                           Guid workflowInstanceID)
        {
            ISharePointService service = (ISharePointService)context.GetService(typeof(ISharePointService));

            if (service == null)
            {
                throw new InvalidOperationException();
            }

            service.LogToHistoryList(workflowInstanceID, SPWorkflowHistoryEventType.WorkflowError, 0, TimeSpan.MinValue,
                                     "Error", e.ToString(), "");
        }

        /// <summary>
        /// Adds a comment to the workflow history
        /// </summary>
        /// <param name="message">Message to add</param>
        /// <param name="context">Current ActivityExecutionContext</param>
        /// <param name="workflowInstanceId">Current WorkflowInstanceId</param>
        internal static void AddCommentWorkflowHistory(string message, ActivityExecutionContext context,
                                                       Guid workflowInstanceId)
        {
            ISharePointService service = (ISharePointService)context.GetService(typeof(ISharePointService));

            if (service == null)
            {
                throw new InvalidOperationException();
            }

            service.LogToHistoryList(workflowInstanceId, SPWorkflowHistoryEventType.None, 0, TimeSpan.MinValue, "",
                                     message, message);
        }

        internal static Microsoft.Office.Word.Server.Service.WordServiceApplicationProxy GetWordServiceApplicationProxy(Microsoft.SharePoint.Administration.SPWebApplication webApplication)
        {
            if (!webApplication.ServiceApplicationProxyGroup.ContainsType(typeof(WordServiceApplicationProxy)))
            {
                throw new Exception("No WordServiceApplicationProxy associated with this SPWebApplication");
            }
            else
            { 
                foreach (SPServiceApplicationProxy proxy in webApplication.ServiceApplicationProxyGroup.Proxies)
                {
                    if (proxy.GetType() == typeof(WordServiceApplicationProxy))
                        return (WordServiceApplicationProxy)proxy;
                }
                return null;
            }
        }
    }
}
